﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterDisplay : MonoBehaviour
{
    public int id;
    public Animator animator;
    public Vector3 effectSpanwOffset;
    public float bulletSpeed;
    private void Awake()
    {
        animator = GetComponent<Animator>();
    }
    /// <summary>
    /// 攻击显示
    /// </summary>
    /// <param name="id">使用的攻击的ID</param>
    /// <returns>攻击动画的时长</returns>
    public float Attack(int id)
    {
        AttackInfo attackInfo = AttackFactory.Instance.GetAttackInfo(id);
        animator.SetTrigger(attackInfo.animatTriggetName);
        
        //获取攻击动画的时长
        AnimationClip[] clips = animator.runtimeAnimatorController.animationClips;
        foreach (var item in clips)
        {
            if (attackInfo.animatClipName == item.name)
            {
                return item.length;
            }
        }
        return 0;
    }


    public void ShowAttackEffect(int id, GameObject targetObj)
    {
        AttackInfo attackInfo = AttackFactory.Instance.GetAttackInfo(id);
        //实例化攻击特效
        if (attackInfo.effect)
        {
            GameObject projectile = Instantiate(attackInfo.effect, transform.position + effectSpanwOffset, Quaternion.identity) as GameObject;
            projectile.transform.LookAt(targetObj.transform.position + effectSpanwOffset);
            projectile.GetComponent<Rigidbody>().AddForce(projectile.transform.forward * bulletSpeed);
            Bullet ps = projectile.GetComponent<Bullet>();
            ps.impactNormal = targetObj.transform.position + effectSpanwOffset;
            ps.owner = gameObject;
        }
    }

    public void Hit()
    {
        animator.SetTrigger("Hit");
    }

    public void Die()
    {
        animator.SetTrigger("Die");
    }
}
